package com.renwei.yunlong.activity.work;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.GuideListAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.GuideAllBean;
import com.renwei.yunlong.bean.GuideCateInfo;
import com.renwei.yunlong.bean.GuideInfo;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.view.SimpleOptionView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class GuideListActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener, OnRefreshListener, GuideListAdapter.GuideClickListener {


    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private GuideListAdapter guideListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_list);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        initView();
    }

    private void initView() {
        simpleTileView.setTitle("使用引导");
        guideListAdapter = new GuideListAdapter(this);
        guideListAdapter.setOnclickListener(this);
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnRefreshListener(this);
        recyclerView.setAdapter(guideListAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("2".equals(companyType)) {
            map.put("userName", StringUtils.value(serviceLoginBean.getRows().getUserName()));
            map.put("serviceProviderCode", StringUtils.value(serviceLoginBean.getRows().getServiceProviderCode()));
        } else {
            map.put("userName", StringUtils.value(ownerBean.getRows().getUserName()));
            map.put("ownerCode", StringUtils.value(ownerBean.getRows().getOwnerCode()));
        }
        ServiceRequestManager.getManager().getGuideList(this, JsonMapUtil.mapToJson(map), this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }


    @Override
    public void onSuccess(int requestId, String result) {
        List<GuideCateInfo> list = new ArrayList<>();
        int total = 0;
        int current = 0;
        GuideAllBean guideBean = new Gson().fromJson(result, GuideAllBean.class);
        for (String key : guideBean.getRows().keySet()) {
            GuideCateInfo guideInfoTitle = new GuideCateInfo();
            if ("1".equals(key)) {
                guideInfoTitle.setCateName("报障工单");
            } else if ("2".equals(key)) {
                guideInfoTitle.setCateName("邀请企业");
            } else if ("3".equals(key)) {
                guideInfoTitle.setCateName("了解对象");
            } else if ("4".equals(key)) {
                guideInfoTitle.setCateName("了解项目");
            }
            list.add(guideInfoTitle);
            List<GuideInfo> guideInfos = guideBean.getRows().get(key);
            for (GuideInfo info : guideInfos) {
                GuideCateInfo guideInfoData = new GuideCateInfo();
                guideInfoData.setGuideInfo(info);
                list.add(guideInfoData);
                if ("1".equals(info.getModelFlag())) {
                    current += 1;
                }
                total += 1;
            }
        }
        guideListAdapter.setData(list);
        refreshLayout.finishRefresh();
        simpleTileView.setTitle(StringUtils.value("使用引导" + "（" + current + "/" + total + "）"));
    }

    @Override
    public void onException(int requestId, String code) {

    }

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, GuideListActivity.class);
        context.startActivity(intent);
    }


    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        initData();
    }

    @Override
    public void onGuideClick(GuideInfo info) {
        GuideWebActivity.openActivity(this, info);

    }
}
